System.Build = function(targetPath, sourcePath, templatePath, consoleBuild, win32build) {
	var supportFolder = Dir.applicationSupport + "/iCode"
	var objFiles = []
	var gccpath
	var currentVersion = System.appVersion
	var appVerPath = supportFolder + "/appVer";
	
	if (win32build)
		gccpath = "/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-gcc";
	else
		gccpath = "/usr/bin/gcc";

	if (currentVersion != null && File.exists(appVerPath)) {
		var lastVersion = File.read(appVerPath)
		if (lastVersion != currentVersion) {
			Dir.remove(supportFolder)
		}
	}
	
	if (!File.exists(supportFolder))
		Dir.create(supportFolder)

	if (currentVersion != null)
		File.write(appVerPath, currentVersion);
		
	if (consoleBuild) {
		supportFolder += "/consoleBuild"
	} else {
		supportFolder += "/appBuild"
	}
	
	if (win32build)
		supportFolder += "W32";

	if (!File.exists(supportFolder))
		Dir.create(supportFolder)

	function compileFile(srcFile, objFile) {
		var srcInfo = File.info(srcFile)
		var destInfo = File.info(objFile)
		
		objFiles.push(objFile)
		
		if (srcInfo != null && destInfo != null) {
			if (destInfo.modified.getTime() > srcInfo.modified.getTime())
				return;
		}
		
		var args = ["-c", "-std=c99"];
		if (win32build)
			args.push("-DWIN32");
		else
			args.push("-DMACOSX");
		if (consoleBuild)
			args.push("-DCONSOLE")
		if (!win32build)
			args.push("-includeCocoa/Cocoa.h")
		args.push("-include" + templatePath + "/jsruntime.h",
	            "-I" + templatePath, "-I/usr/include/libxml2", "-o", objFile, srcFile)  
		
		return System.execute({
			path: gccpath,
			arguments: args
		})
	}

	function compileICMFile(name) {
		var srcFile = templatePath + "/" + name + ".m"
		var objFile = supportFolder + "/" + name + ".o"
		compileFile(srcFile, objFile)
	}

	function buildOSXApplication(path, template) {
		Dir.create(path)
		var contents = path + "/Contents"
		Dir.create(contents)
		var resources = contents + "/Resources"
		var resources = contents + "/Resources"
		Dir.create(resources)
		Dir.create(contents + "/MacOS")
		
		File.copy(template + "/Contents/Info.plist", contents + "/Info.plist")
		File.copy(template + "/Contents/Resources/English.lproj/MainMenu.nib", resources + "/MainMenu.nib")
	}
	
	var icmFolder = "/Users/andrew/iCodeMac/iCodeMacRun/";
		
	var linkArgs = ["-std=c99",
		            "-include" + templatePath + "/jsruntimecore.h",
		            "-o", targetPath, sourcePath]
	
	compileICMFile("jsruntime")
	if (!consoleBuild) {
		compileICMFile("form")
		compileICMFile("control")
		compileICMFile("NSRadioButtonICM")
		compileICMFile("ActionBridge")
		compileICMFile("FlippedView")
		compileICMFile("CanvasView")
		compileICMFile("TableView")
		compileICMFile("BrowserView")
		compileICMFile("AppLoader")
		compileICMFile("timer")
	}
	compileICMFile("mersenneTwister")
	if (!win32build) {
		compileICMFile("task")
		compileICMFile("files")
		compileICMFile("url")
		compileICMFile("xml")
		compileICMFile("graphics")
		compileICMFile("cencode")
		compileICMFile("DebugClient")
		compileICMFile("WrappedJsval")
	} else {
		compileICMFile("nullDebugClient")
	}
		
	compileICMFile("main")

	for (var i = 0; i < objFiles.length; i++) {
		linkArgs.push(objFiles[i])
	}

	if (win32build) {
	} else {
		linkArgs.push("-framework", "cocoa", "-lc", "-lcurl", "-lxml2")
	}

		
	var result = System.execute({
		path: gccpath,
		arguments: linkArgs
	})
}
